package com.fp.m7.client.widget;

import com.fp.m7.client.MyMessages;
import com.fp.m7.shared.Constants;
import com.fp.m7.shared.dto.AccountDto;
import com.fp.m7.shared.dto.UserDto;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.ToggleButton;

/**
 * A widget for displaying some basic info about the account / user.
 * 
 * @author 7
 */
public class AccountInfoWidget extends Composite {

    private static MyMessages msg = GWT.create(MyMessages.class);

    private Image image;
    private Label lblNickname;
    private Label lblStatus;
    private TextArea taDescription;
    private Label lblIsFollowing;
    private Label lblIsFollowedBy;
    private ToggleButton tglbtnFollow;

    public AccountInfoWidget() {
        AbsolutePanel absolutePanel = new AbsolutePanel();
        initWidget(absolutePanel);
        absolutePanel.setSize("449px", "272px");

        image = new Image(Constants.DEFAULT_AVATAR_URL);
        absolutePanel.add(image, 10, 10);
        image.setSize("100px", "100px");

        lblNickname = new Label();
        absolutePanel.add(lblNickname, 116, 10);
        lblNickname.setSize("269px", "36px");

        lblStatus = new Label();
        absolutePanel.add(lblStatus, 116, 74);
        lblStatus.setSize("324px", "36px");
        lblStatus.setWordWrap(true);

        Label lblDescriptionLabel = new Label(msg.canBeDescribedAs());
        absolutePanel.add(lblDescriptionLabel, 10, 116);
        lblDescriptionLabel.setSize("430px", "18px");

        Label lblIsInMoodLabel = new Label(msg.isInMood());
        absolutePanel.add(lblIsInMoodLabel, 116, 52);
        lblIsInMoodLabel.setSize("324px", "18px");

        taDescription = new TextArea();
        taDescription.setReadOnly(true);
        taDescription.addStyleName("notResizable");
        absolutePanel.add(taDescription, 10, 140);
        taDescription.setSize("430px", "72px");

        lblIsFollowing = new Label();
        absolutePanel.add(lblIsFollowing, 10, 218);
        lblIsFollowing.setSize("430px", "18px");

        lblIsFollowedBy = new Label();
        absolutePanel.add(lblIsFollowedBy, 10, 242);
        lblIsFollowedBy.setSize("430px", "18px");

        tglbtnFollow = new ToggleButton(); // up face ==> Follow; down face ==> Unfollow
        absolutePanel.add(tglbtnFollow, 391, 10);
        tglbtnFollow.setStyleName("buttonFollow");
        tglbtnFollow.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
            @Override
            public void onValueChange(ValueChangeEvent<Boolean> evt) {
                if (evt.getValue() != null) {
                    tglbtnFollow.setStyleName(evt.getValue() ? "buttonUnfollow" : "buttonFollow");
                }
            }
        });

        setData(null);
    }

    public void setData(AccountDto accountDto) {
        if (accountDto != null) {
            UserDto userDto = accountDto.getUserDto();
            image.setUrl(userDto.getAvatarUrl() == null ? Constants.DEFAULT_AVATAR_URL : userDto.getAvatarUrl());
            lblNickname.setText(userDto.getNickname());
            lblStatus.setText(accountDto.getStatus());
            taDescription.setText(accountDto.getDescription());

            setFollowersCount(accountDto.getFollowersCount());
            setFollowingCount(accountDto.getFollowingCount());
        } else {
            // reset all fields to defaults
            image.setUrl(Constants.DEFAULT_AVATAR_URL);
            lblNickname.setText("");
            lblStatus.setText("");
            taDescription.setText("");

            setFollowersCount(null);
            setFollowingCount(null);

            tglbtnFollow.setVisible(false);
        }
    }

    public void setFollowersCount(Integer count) {
        lblIsFollowedBy.setText(msg.isFollowedBy().replace("{0}", "" + count));
        lblIsFollowedBy.setVisible(count != null);
    }

    public void setFollowingCount(Integer count) {
        lblIsFollowing.setText(msg.isFollowing().replace("{0}", "" + count));
        lblIsFollowing.setVisible(count != null);
    }

    public ToggleButton getFollowToggleButton() {
        return tglbtnFollow;
    }

    public HasText getDescription() {
        return taDescription;
    }

    public HasText getStatus() {
        return lblStatus;
    }
}
